package com.zhiche.lisa.bms.service.pay.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.pay.PayAffirmApplyMapper;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirmApply;
import com.zhiche.lisa.bms.service.pay.PayAffirmApplyService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 付款确认明细-支付申请 服务实现类
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-10-11
 */
@Service
public class PayAffirmApplyServiceImpl extends ServiceImpl<PayAffirmApplyMapper, PayAffirmApply> implements PayAffirmApplyService {
    @Override
    public void insertPayAffirmApply(PayAffirmApply payAffirmApply) {
        this.insert(payAffirmApply);
    }

    @Override
    public void editPayAffirmApply(List<PayAffirmApply> payAffirmApplies, Long headId) {
        List<PayAffirmApply> payAffirmApplysByHead = this.getPayAffirmApplyByHeadId(headId);
        List<Long> oldPayAffirms = new ArrayList<>();
        Map<Long, PayAffirmApply> payAffirmApplyMap = new HashMap<>();
        List<PayAffirmApply> updatePay = new ArrayList<>();
        List<PayAffirmApply> addPay = new ArrayList<>();

        if (!CollectionUtils.isEmpty(payAffirmApplysByHead)) {
            payAffirmApplysByHead.forEach(payAffirmApply -> {
                oldPayAffirms.add(payAffirmApply.getId());
                payAffirmApplyMap.put(payAffirmApply.getId(), payAffirmApply);
            });
        } else {
            addPay = payAffirmApplies;
        }
        if (!CollectionUtils.isEmpty(payAffirmApplies)) {
            for (PayAffirmApply payAffirmApply : payAffirmApplies) {
                if (oldPayAffirms.contains(payAffirmApply.getId())) {
                    payAffirmApply.setPaidSum(payAffirmApply.getPaidSum().add(payAffirmApplyMap.get(payAffirmApply.getId()).getPaidSum()));

                    updatePay.add(payAffirmApply);
                    oldPayAffirms.remove(payAffirmApply.getId());
                } else {
                    addPay.add(payAffirmApply);
                }
            }
            if (!CollectionUtils.isEmpty(addPay)) {
                this.insertBatch(addPay);
            }
            if (!CollectionUtils.isEmpty(updatePay)) {
                this.updateBatchById(updatePay);
            }
            if (!CollectionUtils.isEmpty(oldPayAffirms)) {
                this.deleteBatchIds(oldPayAffirms);
            }
        } else {
            this.deletePayAffirmApplyByHeadId(headId);
        }
    }

    @Override
    public void deletePayAffirmApplyByHeadId(Long headId) {
        EntityWrapper ew = new EntityWrapper();
        ew.eq("head_id", headId);
        this.delete(ew);
    }

    @Override
    public List<PayAffirmApply> queryPayAffirmApplyAll(List<Long> payIds) {
        EntityWrapper ew = new EntityWrapper();
        ew.in("pay_apply_id", payIds);
        return this.selectList(ew);
    }

    @Override
    public List<PayAffirmApply> getPayAffirmApplyByHeadId(Long headId) {
        EntityWrapper ew = new EntityWrapper();
        ew.eq("head_id", headId);
        return this.selectList(ew);
    }

    /**
     * 根据主表ID，得到付款确认明细-支付申请
     *
     * @param headIdList
     * @return
     */
    @Override
    public List<PayAffirmApply> getPayAffirmApplyByHeadIdList(List<Long> headIdList) {
        if (!CollectionUtils.isEmpty(headIdList)) {
            EntityWrapper ew = new EntityWrapper();
            ew.in("head_id", headIdList);
            return this.selectList(ew);
        }
        return null;
    }
}
